﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonCompareTool
{
    public enum CompareResult
    {
        UnCompared=0,
        Equal=1,
        FirstOneIsBetter=2,
        LastOneIsBetter=3,
        BothLike=4,
        BothDisLike=5
    }

    public delegate void CompareHandle(ComparePair pair,int index,int SumCount);
    public delegate void CompareCompeltedHandle(CompareRank rank);

    public class CompareGroup
    {
        public event CompareHandle CompareEvent;

        public event CompareCompeltedHandle CompareCompleted;

        List<CompareItem> items = new List<CompareItem>();

        public void SetCompareItems(List<string> compareStrList)
        {
            items = new List<CompareItem>();

            if (compareStrList == null || compareStrList.Count <= 0)
            {
                return;
            }

            for (int i = 0; i < compareStrList.Count; i++)
            {
                items.Add(new CompareItem() { Point = 0, Item = compareStrList[i] });
            }

            BuildCompareGroup();
        }

        List<ComparePair> ComparePairs = new List<ComparePair>();

        private void BuildCompareGroup()
        {
            ComparePairs = new List<ComparePair>();

            foreach (CompareItem item in items)
            {
                foreach (CompareItem otheritem in items)
                {
                    if (item.Item==otheritem.Item)
                    {
                        continue;
                    }

                    if (Contains(ComparePairs,item,otheritem))
                    {
                        continue;
                    }

                    ComparePairs.Add(new ComparePair() { ItemA = item, ItemB = otheritem,Result=CompareResult.UnCompared });
                }
            }
        }

        private bool Contains(List<ComparePair> ComparePairs, CompareItem item, CompareItem otheritem)
        {
            return ComparePairs.Any(
                                    x =>
                                         (x.ItemA.Item == item.Item && x.ItemB.Item == otheritem.Item)
                                         ||
                                         (x.ItemB.Item == item.Item && x.ItemA.Item == otheritem.Item)
                                    );
        }

        public void SetCompareResult(int ComparePairIndex,CompareResult result)
        {
            try
            {
                ComparePairs[ComparePairIndex].Result = result;
            }
            catch 
            {
                
                throw;
            }

        }

        int currentIndex = 0;

        internal void StartCompare()
        {
            InvokeShow();
        }

        private void InvokeShow()
        {
            CompareEvent(ComparePairs[currentIndex], currentIndex, ComparePairs.Count);
        }

        internal void SetCompareResult(CompareResult compareResult)
        {
            ComparePairs[currentIndex].Result = compareResult;
            Next();
        }

        private void Next()
        {
            currentIndex++;
            if (ReachEnd())
            {
                CompareCompleted(GetRank());
                return;
            }
            InvokeShow();
        }

        private bool ReachEnd()
        {
            return currentIndex >= ComparePairs.Count;
        }

        private CompareRank GetRank()
        {
            CompareRank rank = new CompareRank();

            List<CompareItem> items = new List<CompareItem>();
            ComparePairs.ForEach(
                x => rank.SetResult(x)
                );

            return rank;

        }






    }
}
